//
//  NSString+Extend.m
//  MexicoLoan
//
//  Created by WXP on 2025/2/26.
//

#import "NSString+Extend.h"

@implementation NSString (Extend)

#pragma mark - 去掉字符串首尾的空格
- (NSString *)removeHeadAndTailSpace{
    // 去掉字符串首尾的空格
    NSString *str = self;
    NSString *trimmedStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return trimmedStr;
}

#pragma mark - 是否只包含空格
- (BOOL) isOnlyBlankSpace{
    NSString *str = self;
    NSString *trimmedStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([trimmedStr length] == 0) {
        return true;
    } else {
        return false;
    }
}

#pragma mark - 不同颜色
- (NSMutableAttributedString *)setDifferentColorWithMarkStr:(NSString *)markStr lineSpacing:(CGFloat)lineSpacing alignment:(NSInteger)alignment color:(UIColor *)color{
    if(!isStr(self)){
        return nil;
    }
    NSRange range = [self rangeOfString:markStr];
    NSValue * value = [NSValue valueWithRange:range];
    NSMutableArray * array = [[NSMutableArray alloc]initWithObjects:value, nil];
    NSMutableAttributedString * detailAttrString = [[NSMutableAttributedString alloc]initWithString:self];
    NSMutableParagraphStyle * detailParagtaphStyle = [[NSMutableParagraphStyle alloc]init];
    detailParagtaphStyle.alignment = alignment;
    detailParagtaphStyle.lineSpacing = lineSpacing;
    NSDictionary * detaiDic = @{
        NSParagraphStyleAttributeName : detailParagtaphStyle,
        NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]
    };
    [detailAttrString addAttributes:detaiDic range:NSMakeRange(0, detailAttrString.length)];
    for (NSValue * rangeVal in array){
        [detailAttrString addAttributes:@{NSForegroundColorAttributeName : color} range:rangeVal.rangeValue];
    }
    return detailAttrString;
}

#pragma mark - 文字+图片
- (NSMutableAttributedString *)setImageName:(NSString *)imageName imageRect:(CGRect)rect withIndex:(NSUInteger)index{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping; // 按字符进行换行 （NSLineBreakByWordWrapping按单词换行）
    NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyle};
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    attch.image = [UIImage imageNamed:imageName];
    attch.bounds = rect;
    NSAttributedString * string1 = [NSAttributedString attributedStringWithAttachment:attch];
    NSMutableAttributedString * tittleAttr = [[NSMutableAttributedString alloc]initWithString:self attributes:attributes];
    [tittleAttr insertAttributedString:string1 atIndex:index];
    return tittleAttr;
}



@end
